package com.woyou.ui.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.Info;
import com.woyou.bean.MyAddress;
import com.woyou.ui.activity.PutAddrActivity_;
import com.woyou.utils.Utils;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 管理地址数据适配器
 * @author tlc
 *
 */
public class AddrListAdapter extends BaseAdapter implements OnClickListener {

	private List<MyAddress> items;
	private Context mContext;
	
	public AddrListAdapter(List<MyAddress> items, Context context) {
		this.items = items;
		mContext = context;
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if( convertView == null ){
			convertView = View.inflate(mContext, R.layout.item_myaddr, null);
			holder = new ViewHolder();
			holder.selectRl = (RelativeLayout) convertView.findViewById(R.id.addr_select_rl);
			holder.address = (TextView) convertView.findViewById(R.id.address);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.sex = (TextView) convertView.findViewById(R.id.sex);
			holder.phone = (TextView) convertView.findViewById(R.id.phone);
			holder.updatebtn = (LinearLayout)convertView.findViewById(R.id.updatebtn);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		final int itemNo = position;
		MyAddress item = items.get(position);
		
		holder.selectRl.setTag(item);
		holder.selectRl.setOnClickListener(this);
		
		holder.item = item;
		
		if(holder.item.getContact()!=null && !holder.item.getContact().equals(""))
		{
			holder.name.setText(holder.item.getContact());
		}
		else
		{
			holder.name.setVisibility(View.GONE);
		}
		System.out.println("这是第"+position+"条数据");
		if(holder.item.getSex()==2)
		{
			holder.sex.setVisibility(View.VISIBLE);
			holder.sex.setText("先生");
		}
		else if(holder.item.getSex()==1)
		{
			holder.sex.setVisibility(View.VISIBLE);
			holder.sex.setText("女士");
		}
		else
		{
			holder.sex.setText("");
			holder.sex.setVisibility(View.GONE);
		}
		holder.phone.setText("（"+holder.item.getPhone()+"）");
		holder.address.setText(holder.item.getAddrName());
		holder.updatebtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//向购物车添加数据
				myAddress = items.get(itemNo);
				clazz = PutAddrActivity_.class;
				fmInfoBean = new Info<MyAddress>(myAddress);
				Utils.openActivity(mContext, clazz, fmInfoBean);
			}
		});
		return convertView;
	}
	
	/**
	 * ViewHolder
	 * @author longtao.li
	 */
	static class ViewHolder{
		RelativeLayout selectRl;
		TextView name;
		TextView sex;
		TextView phone;
		TextView address;
		LinearLayout updatebtn;
		MyAddress item;
	}
	
	Class clazz;
	Info<MyAddress> fmInfoBean = null;
	MyAddress myAddress = null;
	
	//返回UI要显示的距离字符串
	private String getDisStr(String distance) {
		DecimalFormat df = new DecimalFormat("0.0");
		String disStr = "";
			
			double dis = 0;
			try {
				dis = Float.parseFloat(distance);
				if( dis < 1000 ){
					disStr = (int)(dis/1) + "m";
				}else{
					dis = dis / 1000;
					if( dis > 99 ){
						return ">99km";
					}
					disStr =  df.format(dis) + "km"; 
				}
			} catch (Exception e) {
			}
		return disStr;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.addr_select_rl:
			if( listener!=null ){
				listener.addrSelect((MyAddress)v.getTag());
			}
			break;

		default:
			break;
		}
	}
	private AddrSelectListener listener;
	public interface AddrSelectListener{
		void addrSelect(MyAddress addr);
	}
	public void setAddrSelectListener(AddrSelectListener l){
		this.listener = l;
	}
	
}
